package cn.iocoder.an.module.business.controller.admin.codelist;

import cn.iocoder.an.module.business.controller.admin.storeinit.vo.DrawStoreInitImportVo;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.codelist.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.codelist.CodeListDO;
import cn.iocoder.an.module.business.service.codelist.CodeListService;

@Tag(name = "管理后台 - 编码信息")
@RestController
@RequestMapping("/business/code-list")
@Validated
public class CodeListController {

    @Resource
    private CodeListService codeListService;

    @PostMapping("/create")
    @Operation(summary = "创建编码信息")
    @PreAuthorize("@ss.hasPermission('business:code-list:create')")
    public CommonResult<Long> createCodeList(@Valid @RequestBody CodeListSaveReqVO createReqVO) {
        return success(codeListService.createCodeList(createReqVO));
    }

    @PostMapping("/createFail")
    @Operation(summary = "创建编码信息")
    @PreAuthorize("@ss.hasPermission('business:code-list:create')")
    public CommonResult<Long> createFail(@Valid @RequestBody CodeListSaveReqVO createReqVO) {
        return success(codeListService.createFailCodeList(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新编码信息")
    @PreAuthorize("@ss.hasPermission('business:code-list:update')")
    public CommonResult<Boolean> updateCodeList(@Valid @RequestBody CodeListSaveReqVO updateReqVO) {
        codeListService.updateCodeList(updateReqVO);
        return success(true);
    }

    @PutMapping("/check")
    @Operation(summary = "更新编码信息")
    @PreAuthorize("@ss.hasPermission('business:code-list:update')")
    public CommonResult<Map<String,Object>> check(@Valid @RequestBody CodeListSaveReqVO updateReqVO) {
        CodeListDO codeList = BeanUtils.toBean(updateReqVO, CodeListDO.class);
       return success(codeListService.checkCode(codeList));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除编码信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:code-list:delete')")
    public CommonResult<Boolean> deleteCodeList(@RequestParam("id") Long id) {
        codeListService.deleteCodeList(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除编码信息")
                @PreAuthorize("@ss.hasPermission('business:code-list:delete')")
    public CommonResult<Boolean> deleteCodeListList(@RequestParam("ids") List<Long> ids) {
        codeListService.deleteCodeListListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得编码信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:code-list:query')")
    public CommonResult<CodeListRespVO> getCodeList(@RequestParam("id") Long id) {
        CodeListDO codeList = codeListService.getCodeList(id);
        return success(BeanUtils.toBean(codeList, CodeListRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得编码信息分页")
    @PreAuthorize("@ss.hasPermission('business:code-list:query')")
    public CommonResult<PageResult<CodeListRespVO>> getCodeListPage(@Valid CodeListPageReqVO pageReqVO) {
        PageResult<CodeListDO> pageResult = codeListService.getCodeListPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CodeListRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出编码信息 Excel")
    @PreAuthorize("@ss.hasPermission('business:code-list:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCodeListExcel(@Valid CodeListPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CodeListDO> list = codeListService.getCodeListPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "编码信息.xls", "数据", CodeListRespVO.class,
                        BeanUtils.toBean(list, CodeListRespVO.class));
    }

}